home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / jDictionary 1.8 / jdictionary-1_8-win.exe / src-1_8 / info / jdictionary / NewsChecker.java < prev    next >
Encoding:
Java Source  |  2002-07-01  |  2.8 KB  |  96 lines

  1. /*
  2.  * 01/09/2002 - 20:43:57
  3.  *
  4.  * NewsChecker.java -
  5.  * Copyright (C) 2002 Csaba KertΘsz
  6.  * kcsaba@jdictionary.info
  7.  * www.jdictionary.info
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU Lesser General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU Lesser General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. package info.jdictionary;
  25.  
  26. import java.net.URL;
  27. import java.util.Vector;
  28. import java.io.DataInputStream;
  29. import info.jdictionary.events.NewsEvent;
  30. import info.jdictionary.listeners.NewsListener;
  31.  
  32. public class NewsChecker extends Thread {
  33.  
  34.     private URL url;
  35.     private JDictionary jDictionary;
  36.     private int latestVersion;
  37.     private Vector newsListeners = new Vector();
  38.  
  39.     public NewsChecker(JDictionary jDictionary) {
  40.         this.jDictionary = jDictionary;
  41.         try {
  42.             this.url = new URL(JDictionary.getString("InfoServerURL") + "/" + JDictionary.getString("LatestNewsFile"));
  43.         }
  44.         catch(java.net.MalformedURLException e) {this.url = null;}
  45.     }
  46.  
  47.  
  48.     public void run() {
  49.         latestVersion = getLatestVersion();
  50.         if(latestVersion > jDictionary.getPrefs().lastCheckedNewsVersion)
  51.             notifyNewsListeners(latestVersion);
  52.     }
  53.  
  54.  
  55.     private int getLatestVersion() {
  56.         if(url == null)
  57.             return 0;
  58.         byte[] b = new byte[32];
  59.         try {
  60.             DataInputStream dis = new DataInputStream(url.openStream());
  61.             dis.read(b);
  62.         } catch (java.io.IOException e) {
  63.             return 0;
  64.         }
  65.         Float f = new Float(new String(b));
  66.         return f.intValue();
  67.     }
  68.  
  69.  
  70.     public int getLatest() {
  71.         return latestVersion;
  72.     }
  73.  
  74.  
  75.     void notifyNewsListeners(int latestVersion) {
  76.         Vector tmpList;
  77.         String newsURL = jDictionary.getString("NewsPage");
  78.         NewsEvent event = new NewsEvent(this, latestVersion, newsURL);
  79.         synchronized(this) {
  80.             tmpList = (Vector)newsListeners.clone();
  81.         }
  82.         for(int i = 0; i < tmpList.size(); i++) {
  83.             ((NewsListener)tmpList.elementAt(i)).newsUpdated(event);
  84.         }
  85.     }
  86.  
  87.  
  88.     public synchronized void addNewsListener(NewsListener listener) {
  89.         newsListeners.addElement(listener);
  90.     }
  91.  
  92.  
  93.     public synchronized void removeNewsListener(NewsListener listener) {
  94.         newsListeners.removeElement(listener);
  95.     }
  96. }